/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fullalbum.album;

import fullalbum.bean.Figure;
import fullalbum.bean.UserAlbum;
import fullalbum.dao.UserAlbumDAO;
import fullalbum.dao.FigureDAO;
import fullalbum.GenericServlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author leoomoreira
 */
public class ShowUserAlbumServlet extends GenericServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String userId = request.getParameter("userId");
        String albumId = request.getParameter("albumId");

        HttpSession session = request.getSession();

        UserAlbumDAO userAlbumDAO = new UserAlbumDAO();
        UserAlbum userAlbum = userAlbumDAO.getByUserIdAndAlbumId(Integer.parseInt(userId), Integer.parseInt(albumId));

        request.setAttribute("userAlbum", userAlbum);

        FigureDAO figureDAO = new FigureDAO();

        List<Figure> figureList = figureDAO.getFiguresByAlbum(Integer.parseInt(albumId));
        request.setAttribute("figureList", figureList);

        List<Figure> figureWithinAlbumList = figureDAO.getFiguresWithinAlbum(Integer.parseInt(userId), Integer.parseInt(albumId));
        request.setAttribute("figureWithinAlbumList", figureWithinAlbumList);
        List<Figure> repeatedFigureWithinAlbumList = figureDAO.getRepeatedFiguresWithinAlbum(Integer.parseInt(userId), Integer.parseInt(albumId));
        request.setAttribute("repeatedFigureWithinAlbumList", repeatedFigureWithinAlbumList);

        List<Figure> figuresUserLoggedList = figureDAO.getFiguresWithinAlbum(Integer.parseInt(session.getAttribute("userId").toString()), Integer.parseInt(albumId));
        request.setAttribute("figuresUserLoggedList", figuresUserLoggedList);
        List<Figure> figuresSuggestedByUserLoggedList = figureDAO.getFiguresSuggestedToAnotherUser(Integer.parseInt(session.getAttribute("userId").toString()), Integer.parseInt(userId), Integer.parseInt(albumId));
        request.setAttribute("figuresSuggestedByUserLoggedList", figuresSuggestedByUserLoggedList);
        
        List<String[]> threeUsersFigureCount = figureDAO.getThreeUsersFigureCountByAlbum(Integer.parseInt(albumId));
        request.setAttribute("threeUsersFigureCount", threeUsersFigureCount);
        
        List<String> repeatedFigure = figureDAO.getRepeatedFiguresIdSuggested(Integer.parseInt(session.getAttribute("userId").toString()), Integer.parseInt(userId), Integer.parseInt(albumId));
        String strYouNeed = "";
        for (int i = 0; i < repeatedFigure.size(); i++) {
            strYouNeed += repeatedFigure.get(i);
            if (i != repeatedFigure.size() - 1)
                strYouNeed += ", ";
        }
        String strYouExchange = "";
        List<String> exchangeFigure = figureDAO.getExchangeFiguresIdSuggested(Integer.parseInt(session.getAttribute("userId").toString()), Integer.parseInt(userId), Integer.parseInt(albumId));
        for (int i = 0; i < exchangeFigure.size(); i++) {
            strYouExchange += exchangeFigure.get(i);
            if (i != exchangeFigure.size() - 1)
                strYouExchange += ", ";
        }
        request.setAttribute("strYouNeed", strYouNeed);
        request.setAttribute("strYouExchange", strYouExchange);

        RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/jsp/userAlbum.jsp");
        dispatcher.forward(request, response);
    } 

    // <editor-fold defaultstate="collapsed" desc="Métodos HttpServlet. Clique no sinal de + à esquerda para editar o código.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
